package com.bs.sc.mgr.goods.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.bs.sc.base.utils.AjaxJson;
import com.bs.sc.base.utils.FileUtil;
import com.bs.sc.mgr.goods.entity.Goods;
import com.bs.sc.mgr.goods.service.GoodsService;
import com.bs.sc.recommend.service.RecommendService;

@RestController
@RequestMapping(value = "/goods")
@CrossOrigin
public class GoodsController {

	@Autowired
	private GoodsService goodsService;
	@Autowired
	private RecommendService recommendService;

	/**
	 * 获取推荐商品列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "findRecommendGoodsList", method = RequestMethod.POST)
	public AjaxJson findRecommendGoodsList(HttpServletRequest request) {
		return recommendService.findRecommendGoodsList(request);
	}

	/**
	 * 分页查询商品列表
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "findGoodsList", method = RequestMethod.POST)
	public AjaxJson findGoodsList(int pageNumber, int pageSize) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取分页数据失败");
		try {
			Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
			Page<Goods> page = goodsService.findGoodsList(pageable);
			Map<String, Object> pagaData = new HashMap<>();
			pagaData.put("goodsList", page.getContent());
			pagaData.put("count", page.getTotalElements());
			j.setData(pagaData);
			j.setCode("success");
			j.setMsg("获取分页数据成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 图片上传
	 * 
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "imageUpload", method = RequestMethod.POST)
	public AjaxJson imageUpload(MultipartFile file, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("图片上传失败");
		// 判断上传文件格式
		String fileType = file.getContentType();
		if (fileType.equals("image/jpeg") || fileType.equals("image/png") || fileType.equals("image/jpeg")) {
			String localPath = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\upload";
			File f = new File(localPath);
			if (!f.exists()) {
				f.mkdir();
			}
			// 上传后保存的文件名(需要防止图片重名导致的文件覆盖)
			// 获取文件名
			String fileName = file.getOriginalFilename();
			// 获取文件后缀名
			String suffixName = fileName.substring(fileName.lastIndexOf("."));
			// 重新生成文件名
			fileName = UUID.randomUUID() + suffixName;
			if (FileUtil.upload(file, localPath, fileName)) {
				// 文件存放的相对路径(一般存放在数据库用于img标签的src)
				j.setCode("success");
				j.setMsg("图片上传成功");
				j.setData("http://127.0.0.1:8080/img/" + fileName);
			}
		}
		return j;
	}

	/**
	 * 新增商品或更新
	 * 
	 * @param goods
	 * @return
	 */
	@RequestMapping(value = "addOrUpdateGoods", method = RequestMethod.POST)
	public AjaxJson addOrUpdateGoods(Goods goods) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("新增商品失败");
		try {
			goodsService.addOrUpdateGoods(goods);
			j.setCode("success");
			j.setMsg("新增商品成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 删除商品
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "deleteGoods", method = RequestMethod.POST)
	public AjaxJson deleteGoods(String id) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("删除商品失败");
		try {
			Goods goods = goodsService.getGoods(id);
			boolean b = goodsService.deleteGoodsPic(goods);
			if (b) {
				goodsService.deleteGoods(goods);
				j.setCode("success");
				j.setMsg("删除商品成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 获取商品信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getGoods", method = RequestMethod.GET)
	public AjaxJson getGoods(String id) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取商品信息失败");
		try {
			Goods goods = goodsService.getGoods(id);
			j.setCode("success");
			j.setMsg("获取商品信息成功");
			Map<String, Object> result = new HashMap<>();
			List<Map<String, String>> comment = goodsService.findCommentByGoodsId(id);
			result.put("goods", goods);
			result.put("comment", comment);
			j.setData(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 获取精品商品
	 * 
	 * @return
	 */
	@RequestMapping(value = "findBoutiqueGoods", method = RequestMethod.POST)
	public AjaxJson findBoutiqueGoods() {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取精品商品信息失败");
		try {
			List<Goods> boutiqueGoods = goodsService.findBoutiqueGoods();
			j.setCode("success");
			j.setMsg("获取精品商品信息成功");
			j.setData(boutiqueGoods);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 获取所有商品列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "findAllGoods", method = RequestMethod.POST)
	public AjaxJson findAllGoods(String classify) {
		System.out.println(classify + ">>>>>>>>>>>>");
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取商品列表失败");
		try {
			List<Goods> goodsList = null;
			if (StringUtils.isEmpty(classify)) {
				goodsList = goodsService.findAllGoods();
			} else {
				goodsList = goodsService.findByClassify(classify);
			}
			j.setCode("success");
			j.setMsg("获取商品列表成功");
			j.setData(goodsList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 获取订单商品信息
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "getOrderGoods", method = RequestMethod.POST)
	public AjaxJson getOrderGoods(HttpServletRequest request, String ids) {
		return goodsService.getOrderGoods(request, ids);
	}
}
